package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBController;

import model.Booking;
import model.User;
import dao.intf.UserDao;

// Implementation of UserDao
// Connection and manipulation of database goes here.
public class UserDaoImpl implements UserDao {

	// basic flow
	public List<User> getUserList() {
		List<User> userList = new ArrayList<User>();
		
		ResultSet rs = DBController.retrieve("User", null, null);
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					User user = new User();
					user.setId(rs.getString("ID"));
					user.setFirstName(rs.getString("FirstName"));
					user.setLastName(rs.getString("LastName"));
					user.setEmail(rs.getString("Email"));
					user.setContactNo(rs.getString("ContactNo"));
					user.setAddress(rs.getString("Address"));
					
					userList.add(user);
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getUserList() of UserDaoImpl.");
			e.printStackTrace();
		}
		
		return userList;
	}
	
	public User getUserById(String id) {
		User user = new User();
		
		ResultSet rs = DBController.retrieve("User", null, "ID='"+id+"'");
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					user.setId(rs.getString("ID"));
					user.setFirstName(rs.getString("FirstName"));
					user.setLastName(rs.getString("LastName"));
					user.setEmail(rs.getString("Email"));
					user.setContactNo(rs.getString("ContactNo"));
					user.setAddress(rs.getString("Address"));
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getUserById() of UserDaoImpl.");
			e.printStackTrace();
		}
		
		return user;
	}
	public User getUserByEmail(String email) {
		User user = new User();
		
		ResultSet rs = DBController.retrieve("User", null, "Email='"+email+"'");
		
		try {
			if( rs != null ) {
				while( rs.next() ) {
					user.setId(rs.getString("ID"));
					user.setFirstName(rs.getString("FirstName"));
					user.setLastName(rs.getString("LastName"));
					user.setEmail(rs.getString("Email"));
					user.setContactNo(rs.getString("ContactNo"));
					user.setAddress(rs.getString("Address"));
				}
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getUserById() of UserDaoImpl.");
			e.printStackTrace();
		}
		
		return user;
	}
	public int saveUser(User user) {
		int userId=0;
		try {
			if(user!=null) {
				String queryStr	 = " INSERT INTO User ";
						queryStr+= " (FirstName,LastName,Email,Contactno,Address)";
						queryStr+= " VALUES ( ";
						queryStr+= "'"+user.getFirstName()+"',"+"'"+user.getLastName()+"',\n";
						queryStr+= "'"+user.getEmail()+"','"+user.getContactNo()+"','"+user.getAddress()+"')";
				
						userId=DBController.saveAndGetId(queryStr);
				}
			
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at saveBooking BookingDaoImpl.");
			e.printStackTrace();
		}
		
		return userId;
	}
	public int updateUser(User user,String email) {
		int recordCount=0;
		try {
			if(user!=null) {
				String queryStr	 = " Update User ";
						queryStr+= " SET FirstName='"+user.getFirstName()+"',";
						queryStr+= " LastName= '"+user.getLastName()+"',";
						queryStr+= " Email= '"+user.getEmail()+"',";
						queryStr+= " ContactNo= '"+user.getContactNo()+"'";
						queryStr+= " WHERE Email= '"+email+"'";
						
				recordCount=DBController.save(queryStr);
				}
			
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at saveBooking BookingDaoImpl.");
			e.printStackTrace();
		}
		
		return recordCount;
	}

}
